package com.gdcp.controller.portal;

import com.gdcp.beans.*;
import com.gdcp.common.ResponseModel;
import com.gdcp.common.page.PageBean;
import com.gdcp.config.RequestHolder;
import com.gdcp.service.CommentService;
import com.gdcp.service.OrderItemService;
import com.gdcp.service.ProductService;
import com.gdcp.util.GetPcUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

/**
 * @author 杨华京
 * @version 1.0
 * @description
 * @since 1.8
 */
@Controller
@RequestMapping("/render/comment/")
public class CommentController {

    @Autowired
    ResponseModel responseModel;
    @Autowired
    CommentService commentService;
    @Autowired
    OrderItemService orderItemService;
    @Autowired
    ProductService productService;

    //@PreAuthorize("hasAuthority('ROLE_USER')")
    @RequestMapping(value = "/insert",method = RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> insert(String comment,String pid,String oiid){
        //String areacomment=request.getParameter("comment");
        if (pid==null||pid.equals("")){
            return responseModel.setModel(false,"评论失败,pid不能为空");
        }
        if (oiid==null||oiid.equals("")){
            return responseModel.setModel(false,"评论失败,oiid不能为空");
        }

        //String pid=request.getParameter("pid");
        //String oiid=request.getParameter("oiid");

        Comment comment1=new Comment();

        Product product=productService.getInformation(Integer.valueOf(pid));
        if (product==null){
            return responseModel.setModel(false,"商品id不存在");
        }

        comment1.setShop_id(product.getShop_id());
        comment1.setProduct_id(Integer.valueOf(pid));
        comment1.setOrderitem_id(Integer.valueOf(oiid));
        comment1.setParent_id(0);

        User user= (User) RequestHolder.getSession("user");
        comment1.setUser_id(user.getId());
        comment1.setUser_name(user.getUsername());
        comment1.setUser_image(user.getImage());
        comment1.setContent(comment);

        return commentService.insert(comment1);
    }

    //@PreAuthorize("hasAuthority('ROLE_USER')")
    @RequestMapping(value = "/doSupport/{id}",method = RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> doSupport(@PathVariable("id") Integer id){
        int count=commentService.doSupport(id);

        if (count<=0){
            return responseModel.setModel(false,"点赞失败");
        }

        return responseModel.setModel(true,"点赞成功");
    }

    //@PreAuthorize("hasAuthority('ROLE_USER')")
    @RequestMapping(value = "/doOppose/{id}",method = RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> doOppose(@PathVariable("id") Integer id){
        int count=commentService.doOppose(id);
        if (count<=0){
            return responseModel.setModel(false,"踩失败");
        }

        return responseModel.setModel(true,"踩成功");
    }

    //@PreAuthorize("hasAuthority('ROLE_USER')")
    @RequestMapping(value = "/list",method = RequestMethod.GET)
    public String list(){
        //获取Comment、OrderItem
        HttpServletRequest request=RequestHolder.getRequest();
        User user= (User) RequestHolder.getSession("user");
        //List<Comment> comments=commentService.queryListByUID(user.getId());

        int pc = GetPcUtil.getPc(request);
        PageBean<Comment> pb = commentService.queryByPage(user.getId(), pc);
        List<Comment> comments=pb.getBeanList();

        for (Comment comment:comments){
            OrderItem orderItem=orderItemService.getInformation(comment.getOrderitem_id());
            comment.setOrderItem(orderItem);
        }
        pb.setBeanList(comments);
        request.setAttribute("pb",pb);
        //request.setAttribute("comments",comments);
        return "frontend/comment";
    }

    //@PreAuthorize("hasAuthority('ROLE_USER')")
    @RequestMapping(value = "/deleteById",method = RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> deleteById(String id){
        if (id==null||id.equals("")){
            return responseModel.setModel(false,"id不能为空");
        }
        Integer cid= Integer.valueOf(id);
        int count=commentService.deleteById(cid);
        if (count<=0){
            return responseModel.setModel(false,"评论删除失败");
        }
        return responseModel.setModel(true,"评论删除成功");
    }
}
